package com.gopay.scheduler.job;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.constants.batch.BatchPayStep;
import com.gopay.common.constants.batch.BatchProcessStatus;
import com.gopay.common.constants.cbrmb.CbRemitOpStep;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.constants.trans.TxnStaCode;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.domain.cps.CpsBatchPayTask;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.common.domain.cps.cbrmb.Tran01724VO;
import com.gopay.common.domain.cps.transfer.BatchTransferToBank4AirVO;
import com.gopay.common.domain.cps.transfer.BatchTransferToBankVO;
import com.gopay.common.domain.cps.transfer.BatchTransferToGopayVO;
import com.gopay.common.domain.cps.vo.CPSResponse;
import com.gopay.common.order.dao.CommonQueryDAO;
import com.gopay.common.util.DateUtils;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.common.CallCPSHelper;

/**
 * 跨境人民币汇款拆分异步账务处理Job</p>
 * 目前是一分钟执行一次@2017-05-08</p>
 * @CbRmbBatchRemitJob.java
 * @author qld</p>
 * @2017-04-28 上午09:53:14 
 */
public class CbRmbBatchRemitJob extends GopayBaseJob {

    private Logger logger = LoggerFactory.getLogger(CbRmbBatchRemitJob.class);
    
    
    /**
     * 定时任务每隔1分钟执行一次
     */
    private int intervalMinite;
    

    /**
     * 公共查询Dao
     */
    @Resource(name = "commonQueryDAO")
    private CommonQueryDAO commonQueryDAO;

    /**
     * 访问CPS辅助类
     */
    @Resource(name = "callCPSHelper")
    private CallCPSHelper callCPSHelper;

    @Override
    public void doJobInternal() {
       

        // 查询等待执行异步账务的汇款拆分订单
        List<String> taskList = queryWaitingAcpsBatchTasks();
        logger.info("CbRmbBatchRemitJob taskList size :"+taskList.size());

        if (taskList != null && taskList.size() > 0) {
           
            Object paramVo = null;
            CPSResponse response;
            for (String orderId : taskList) {
                paramVo = null;

                    // 汇款拆分异步做账
                    paramVo = build01724BatchParamVo(orderId);
             
                if (paramVo != null) {
                    try {
                        logger.error("-------orderId="+orderId);
                        // 调用cbrcps
                        response=callCPSHelper.callCbRmbByVOTicket(paramVo);
                        if (!ProcCodeConstants.PROC_CODE_100S1000.equals(response.getRespCode())) {
                            logger.error("------调用cbrcps返回异常  response="+response.getRespCode());
                        }

                    } catch (Exception e) {
                        // 吃掉所有异常
                        logger.error("callCbRmbByVOTicket Error", e);
                    }
                }
            }
            
            logger.error("complete task num="+taskList.size());
        }
        
        
    

        
    }

	/**
     * 查询等待执行异步账务的汇款支付总订单
     * 
     * @param pageSize
     * @return
     */
    private List<String> queryWaitingAcpsBatchTasks() {
        List<String> list = new ArrayList<String>();

        StringBuilder sb = new StringBuilder();

        sb.append(" select t.gopay_order_id,t.mer_txn_amt from cps_gen_main_order t ");
        sb.append("  left join cb_rmb_batch_remit_assist c on t.gopay_order_id=c.gopay_order_id ");
        sb.append("         where t.gopay_int_txn_cd= :gopayInTxcd ");
        sb.append("       and  t.txn_sta_cd= :txnStaCd ");
        sb.append("       and c.batch_process_status= :batchStatus ");


        Map<String, Object> map = new HashMap<String, Object>();
        map.put("gopayInTxcd", IntTxnCd._01735.value);
        map.put("txnStaCd", TxnStaCode.TXN_STA_CD_30402.value);
        map.put("batchStatus", BatchProcessStatus.WAITING_ASYNCHRONOUS_ACPS.value);
       

        List<Object[]> rsList = commonQueryDAO.findBySql(sb.toString(), map);
        String r = null;
        if (rsList != null && rsList.size() > 0) {
            for (Object[] arr : rsList) {
                r=(String) arr[0];
                
                list.add(r);
            }
        }

        return list;
    }

    /**
     * 构造批量付款到银行请求参数VO
     * 
     * @param cpsTaskId
     * @return
     */
    private Object build01724BatchParamVo(String orderId) {
        Tran01724VO paramVo = new Tran01724VO();
        paramVo.setCbRemitOpStep(CbRemitOpStep.REMIT_ASYNCHRONOUS);
        paramVo.setGopayOrderId(orderId);

        return paramVo;
    }


    public int getIntervalMinite() {
        return intervalMinite;
    }

    public void setIntervalMinite(int intervalMinite) {
        this.intervalMinite = intervalMinite;
    }
    


}
